(() => {
  let ele = {};
  ele.more = $("#more");
  ele.empty = $("#empty");
  ele.meiyou = $("#meiyou");
  ele.list_son = $("#list_son");
  ele.inp = $("#inp");
  ele.sech = $("#sech");
  let page = 1;
  let list = [];
  let code = "";

  // todo点击搜索

  ele.sech.onclick = () => {
    let val = ele.inp.value;
    if (val.trim().length == 0) {
      openTips("请输入ID搜索");
    } else {
      page = 1;
      list = [];
      code = ele.inp.value;
      qiegejl();
    }
  };
  ele.inp.oninput = () => {
    code = ele.inp.value;
    if (code.trim().length == 0) {
      page = 1;
      list = [];
      qiegejl();
    }
  };
  //   !请求数据
  qiegejl();
  function qiegejl() {
    let token = getCookie("token");
    ajax({
      method: "get",
      url: API_PATH.YQ_FRIEND_LIST,
      header: {
        Authorization: token,
      },

      data: {
        page: page,
        invite_code: code,
        // size: 1,
      },
      success: (res) => {
        if (res.code == 1) {
          // obj = res.data.detail;
          list = [...list, ...res.data.data];
          if (list.length === 0) {
            page = 0;
            ele.more.style.display = "none";
            ele.empty.style.display = "block";
          } else {
            initList();
            page++;
            if (page > res.data.last_page) {
              ele.more.innerText = "没有更多了";
              ele.more.onclick = null;
            } else {
              ele.more.innerText = "加载更多";
              ele.more.onclick = () => qiegejl(page);
            }
          }
        } else openTips(res.msg);
        initList();
      },

      error: (err) => {
        // openTips(err.msg);
      },
    });
  }
  function initList() {
    let newList = list
      .map((item) => {
        return `
          <div class="list">
             <div class="centerin">
                  <img src="${
                    item.detail && item.detail.head_pic
                      ? IMG_DOMAIN + item.detail.head_pic
                      : item.detail && item.detail.sex == 1
                      ? "http://img.qxgs.cc/tiantianlaishouzu/img/my/tx1.png"
                      : "http://img.qxgs.cc/tiantianlaishouzu/img/my/tx2.png"
                  }" alt="" />
              </div>
              <div class="name_tit">
                  <p class="name_t">${
                    item.detail && item.detail.nick_name
                      ? item.detail.nick_name
                      : "无"
                  }</p>
                 <p class="name_b">游戏ID：${item.invite_code}</p>
             </div>
             <div class="name_jin">
                  <p class="name_t">今日贡献</p>
                  <p class="name_b">${item.today_qg ? item.today_qg : 0}</p>
              </div>
             <div class="name_lei">
                 <p class="name_t">累计贡献</p>
                 <p class="name_b">${item.all_qg ? item.all_qg : 0}</p>
             </div>
          </div>
      `;
      })
      .join(" ");
    ele.list_son.innerHTML = newList;
  }
})();
